package com.wantupai.translate.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wantupai.translate.R;
import com.wantupai.translate.activity.camear.CameraActivity;
import com.wantupai.translate.bean.MenuBean;
import com.wantupai.translate.widget.AutoLocateHorizontalView;

import java.util.List;

public class NcTabAdapter extends RecyclerView.Adapter<NcTabAdapter.AgeViewHolder> implements AutoLocateHorizontalView.IAutoLocateHorizontalView {
    private CameraActivity context;
    private View view;
    private List<MenuBean> ages;
    private int selectPos;

    public NcTabAdapter(CameraActivity context, List<MenuBean> ages, int selectPos) {
        this.context = context;
        this.ages = ages;
        this.selectPos = selectPos;
    }

    @NonNull
    @Override
    public AgeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item_tablayout, parent, false);
        return new AgeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final AgeViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.tvAge.setText(ages.get(position).getSimple_name());
        if (selectPos == position) {
            holder.tvAge.setTextColor(Color.parseColor("#736CF9"));
            holder.tvAge.setTextSize(17);
        } else {
            holder.tvAge.setTextColor(Color.WHITE);
            holder.tvAge.setTextSize(13);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                context.selectPos(position);
                selectPos = position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return ages.size();
    }

    @Override
    public View getItemView() {
        return view;
    }

    @Override
    public void onViewSelected(boolean isSelected, int pos, RecyclerView.ViewHolder holder, int itemWidth) {
        selectPos = pos;
        if (isSelected) {
            ((AgeViewHolder) holder).tvAge.setTextColor(Color.parseColor("#736CF9"));
            ((AgeViewHolder) holder).tvAge.setTextSize(17);
        } else {
            ((AgeViewHolder) holder).tvAge.setTextColor(Color.WHITE);
            ((AgeViewHolder) holder).tvAge.setTextSize(13);
        }
    }

    static class AgeViewHolder extends RecyclerView.ViewHolder {
        TextView tvAge;

        AgeViewHolder(View itemView) {
            super(itemView);
            tvAge = itemView.findViewById(R.id.tv_nctab);
        }
    }
}
